import bpy
bpy.data.objects["obj"].data.vertices[0].co.x += 1.0
dir( bpy.data.objects["obj"].data.vertices[0] )
dir( bpy.data.objects["obj"].data.vertices[0].undeformed_co )
help(bpy.data.objects["obj"].data.vertices[0].co)
import sys
out = sys.stdout
sys.stdout = open("f:/help.py","w")
help(bpy.data.objects["obj"].matrix_local)
sys.stdout.close()
sys.stdou = out

from contextlib import contextmanager
f = open("f:/help.py","w")
with redirect_stdout(f):
	help(bpy.data.objects["obj"].matrix_local)

redirect_stdout(f)

bpy.data.objects["obj"].data.vertices.values()

import contextlib
with open('f:/help.txt', 'w') as f:
    with contextlib.redirect_stdout(f):
	    help(bpy.data.objects["obj"].data.vertices)
	    help(bpy.data.objects["obj"].data.vertices[0].co)

		transform
		vertex
		Local
		global
		
print(bpy.data.objects["obj"].matrix_local)
print(bpy.data.objects["obj"].matrix_world)
print(bpy.data.objects["obj"].matrix_basis)
print(bpy.data.objects["obj"].matrix_local)
print(bpy.data.objects["obj"].matrix_local)


bpy.data.objects["obj"].convert_space(to_space= "LOCAL");
print(bpy.data.objects["obj"].data.vertices[0].co)
bpy.data.objects["obj"].convert_space(to_space= "WORLD");
print(bpy.data.objects["obj"].data.vertices[0].co)


def coords(objName, space='GLOBAL'):
	# Store reference to the bpy.data.objects datablock
	obj = bpy.data.objects[objName]
	# Store reference to bpy.data.objects[].meshes datablock
	if obj.mode == 'EDIT':
		v = bmesh.from_edit_mesh(obj.data).verts
	elif obj.mode == 'OBJECT':
		v = obj.data.vertices	
	if space == 'GLOBAL':
		# Return T * L as list of tuples
		return [(obj.matrix_world * v.co).to_tuple() for v in v]
	elif space == 'LOCAL':
		# Return L as list of tuples
		return [v.co.to_tuple() for v in v]

class sel:
	# Add this to the ut.sel class, for use in object mode
	def transform_apply():	
		bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
		
#import ut
import importlib
#importlib.reload(ut)

import bpy

# Will fail if scene is empty
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(0, 0, 0))
bpy.context.object.name = 'Cube-1'

# Check global and local coordinates
print('\nBefore transform:')
print('Global:', coords('Cube-1', 'GLOBAL')[0:2])
print('Local: ', coords('Cube-1', 'LOCAL')[0:2])
# Translate it along x = y = z
# See the cube move in the 3D viewport
bpy.ops.transform.translate(value = (3, 3, 3))
# Check global and local coordinates
print('\nAfter transform, unapplied:')
print('Global: ', coords('Cube-1', 'GLOBAL')[0:2])
print('Local: ', coords('Cube-1', 'LOCAL')[0:2])
# Apply transformation
# Nothing changes in 3D viewport
ut.sel.transform_apply()
# Check global and local coordinates
print('\nAfter transform, applied:')
print('Global: ', coords('Cube-1', 'GLOBAL')[0:2])
print('Local: ', coords('Cube-1', 'LOCAL')[0:2])
############################ Output ###########################
# Before transform:
# Global: [(-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5)]
# Local: [(-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5)]
#
# After transform, unapplied:
# Global: [(2.5, 2.5, 2.5), (2.5, 2.5, 3.5)]
# Local: [(-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5)]
#
# After transform, applied:
# Global: [(2.5, 2.5, 2.5), (2.5, 2.5, 3.5)]
# Local: [(2.5, 2.5, 2.5), (2.5, 2.5, 3.5)]
###############################################################

import bmesh
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bpy.ops.mesh.select_mode(type = "VERT")
bm.verts.ensure_lookup_table()
bm.verts[0].select = True
#bpy.data.objects["obj"].data.vertices[0].
print('Global: ', coords('obj', 'GLOBAL')[1])
print('Local: ', coords('obj', 'LOCAL')[1])

print('Global: ', coords('obj', 'GLOBAL')[5])
print('Local: ', coords('obj', 'LOCAL')[5])